<template>
  <div class="login-container">
    <div class="form-box">
      <h1>黑马面经运营后台</h1>
      <el-form :model="ruleForm" :rules="rules" class="demo-ruleForm" ref="ruleForm" status-icon>
        <el-form-item label="用户名" prop="pass">
          <el-input autocomplete="off" placeholder="请输入用户名" type="password" v-model="ruleForm.pass"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="checkPass">
          <el-input
            autocomplete="off"
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.checkPass"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="submitForm('ruleForm')" class="submit" type="primary">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: 'LoginVue',
  data () {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('年龄不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 18) {
            callback(new Error('必须年满18岁'));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        checkPass: '',
        age: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        age: [
          { validator: checkAge, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style lang='scss'  scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background-color: pink;
  position: relative;
  .form-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 35%;
    background-color: #fff;
    .el-form {
      margin: 60px 40px;
      button {
        font-size: 20px;
        &.submit {
          background-color: #727cf5;
        }

        padding: 18px 30px;
      }
    }
    h1 {
      text-align: center;
      background-color: #727cf5;
      margin: 0;
      padding: 20px 0;
      color: #fff;
      font-weight: 400;
    }
  }
}
</style>